//
//  MyViewController.m
//  FlowerRainGround
//
//  Created by lanou on 16/4/19.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "MyViewController.h"
#import "LoginViewController.h"
#import "SetUserInformationViewController.h"
#import "MyCollectionViewCell.h"
#import "UserCollectionModel.h"
#define kItemWidth (self.view.frame.size.width - 25) / 2
@interface MyViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UICollectionView *collectionVC;
@property (nonatomic, strong) NSMutableArray *collectionModelArray;

@end

@implementation MyViewController

- (NSMutableArray *)collectionModelArray{
    if (!_collectionModelArray) {
        _collectionModelArray = [[NSMutableArray alloc] initWithCapacity:0];
        
    }
    return _collectionModelArray;
}

//请求数据
- (void) requestData
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager GET:[NSString stringWithFormat:@"http://m.htxq.net/servlet/UserCenterServlet?action=getMySubscibeContents&currentPageIndex=0&pageSize=15&userId=%@&(null)=",[userManager getUsrID]] parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves error:nil];
        NSArray *array = dic[@"result"];
        for (NSDictionary *dic1 in array) {
            UserCollectionModel *model = [[UserCollectionModel alloc] init];
            [model setValuesForKeysWithDictionary:dic1];
            [self.collectionModelArray addObject:model];
        }
        NSLog(@"%@", dic);
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.collectionVC reloadData];

        });
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
    }];
}




//当视图出现时如果没有得到用户的ID,就直接跳到登陆界面
- (void)viewWillAppear:(BOOL)animated{
    NSLog(@"%@",[userManager getUsrID]);
    if ([[userManager getUsrID] isEqualToString:@" "]) {
       
        LoginViewController *loginVC = [[LoginViewController alloc] init];
        [self presentViewController:loginVC animated:YES completion:nil];
//       
    }
}




//添加导航栏按钮
- (void)addNavigationBarItem{
    self.navigationItem.title = @"个人中心";
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setBackgroundImage:[UIImage imageNamed:@"chilun"] forState:UIControlStateNormal];
    button.frame = CGRectMake(0, 0, 25, 25);
    button.block = ^(){
        [self popToSetUserInformationView];
    };
    UIBarButtonItem *right = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.rightBarButtonItem = right;
}

//跳转到设置界面
- (void)popToSetUserInformationView
{
    SetUserInformationViewController *userVC = [[SetUserInformationViewController alloc] init];
    [self.navigationController pushViewController:userVC animated:YES];
}


//设置collectionVC;
- (void) creatCollection
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    
    flowLayout.itemSize = CGSizeMake( kItemWidth, 100);
    flowLayout.minimumInteritemSpacing = 15;
    flowLayout.minimumLineSpacing = 0;
    self.collectionVC = [[UICollectionView alloc] initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:flowLayout];
    self.collectionVC.backgroundColor = [UIColor whiteColor];
    self.collectionVC.delegate = self;
    self.collectionVC.dataSource = self;
    
    //注册cell
   
    [self.collectionVC registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    [self.view addSubview:self.collectionVC];
}




- (void)viewDidLoad {
    [super viewDidLoad];
    [self requestData];
    [self addNavigationBarItem];
    
    [self creatCollection];
    
    
    
    
    
    
    
    
    
    // Do any additional setup after loading the view from its nib.
}

#pragma mark--------coledateDelegate-------

- (NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    NSLog(@"aaaa:%ld", self.collectionModelArray.count);
    return self.collectionModelArray.count;
}

- (UICollectionViewCell *) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *reuse = @"reuse";
    UserCollectionModel *model = self.collectionModelArray[indexPath.row];
    MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuse forIndexPath:indexPath];
    NSLog(@"%@", model.title);
    cell.backgroundColor = [UIColor redColor];
    cell.titleLabel.text = @"1111";
    cell.model = model;
    return cell;
}








- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
